/* Copyright © Worklight Ltd. 2006-2011.  All rights reserved. */

package com.worklight.androidgap.plugin;

import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;

import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class Logger extends Plugin {
	 
	private String logCat = "";
					
	public void debug(String msg){
		Log.d(logCat, msg);
	}

	public void error(String msg){
		Log.e(logCat, msg);
	}

	@Override
	public PluginResult execute(String action, JSONArray args, String callbackId) {
		PluginResult.Status status = PluginResult.Status.OK;
		String result = "";		
		
		try {
			if (action.equals("DEBUG")) {
				this.debug(args.getString(0));
			}
			else if (action.equals("ERROR")) {
				this.error(args.getString(0));
			}
			return new PluginResult(status, result);
		} catch (JSONException e) {
			return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
		}
	}

	@Override
	public boolean isSynch(String arg0) {
		return true;
	}

	@Override
	public void setContext(PhonegapActivity droidGap) {
		logCat = droidGap.getClass().getSimpleName();		
	}	
}
